package com.tools.developer;

import java.io.File;
import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.tools.developer.app.AppListActivity;
import com.tools.developer.file.FileOperation;
import com.tools.developer.hardware.HardwareActivity;
import com.tools.developer.root.SuExec;
import com.tools.developer.service.DeveloperService;
import com.tools.developer.ui.ExDialog;

public class MainActivity extends Activity implements OnClickListener {
	private static final int REQUEST_EX = 1;

	private static final int MSG_START_SERVICE = 0x1001;

	private MyHandler mHandler = new MyHandler(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button btn_anr = (Button) this.findViewById(R.id.main_btn_anr);
		btn_anr.setOnClickListener(this);

		Button btn_app = (Button) this.findViewById(R.id.main_btn_app);
		btn_app.setOnClickListener(this);

		((Button) this.findViewById(R.id.main_btn_sysinfo))
				.setOnClickListener(this);
		((Button) this.findViewById(R.id.main_btn_root))
				.setOnClickListener(this);
		mHandler.sendEmptyMessage(MSG_START_SERVICE);
		FileOperation.copy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_btn_anr:

			Intent intent = new Intent();
			intent.putExtra("explorer_title",
					getString(R.string.dialog_read_from_dir));
			intent.setDataAndType(Uri.fromFile(new File("/data/anr/")), "*/*");
			intent.setClass(MainActivity.this, ExDialog.class);
			startActivityForResult(intent, REQUEST_EX);

			break;
		case R.id.main_btn_app:
			startActivity(new Intent(this, AppListActivity.class));
			break;
		case R.id.main_btn_sysinfo:
			startActivity(new Intent(this, HardwareActivity.class));
			break;
		case R.id.main_btn_root:

			SuExec.getInstance().enterRootByMethod();

			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		String path;
		if (resultCode == RESULT_OK) {
			if (requestCode == REQUEST_EX) {
				Uri uri = intent.getData();
				TextView text = (TextView) findViewById(R.id.text);
				text.setText("select: " + uri);
			}
		}
	}

	static class MyHandler extends Handler {
		WeakReference<Activity> mActivity;

		MyHandler(Activity activity) {
			mActivity = new WeakReference<Activity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_START_SERVICE:
				Activity activity = mActivity.get();
				if (activity != null) {
					activity.startService(new Intent(mActivity.get(),
							DeveloperService.class));
				}
				break;

			default:
				break;
			}
		}
	}

}
